package ro.ubbcluj.android.PhysiTrack;

import graphview.GraphView;
import graphview.GraphView.GraphViewData;
import graphview.GraphView.GraphViewSeries;
import graphview.LineGraphView;
import helper.FileIOHelper;

import java.io.IOException;
import java.util.LinkedList;
import java.util.ListIterator;

import android.app.Activity;
import android.app.AlertDialog;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.LinearLayout;

public class DiagramActivity extends Activity {

	private static final String TAG = DiagramActivity.class.getSimpleName();
	private LinkedList<double[]> result;
	private int db;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.requestWindowFeature(Window.FEATURE_NO_TITLE);
		this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
				WindowManager.LayoutParams.FLAG_FULLSCREEN);
		setContentView(R.layout.diagramm);

		String path = getIntent().getExtras().getString("filepath");
		Log.d(TAG, path);
		try {
			result = FileIOHelper.resultFromFile(path);
		} catch (NumberFormatException e) {
			AlertDialog alertDialog = new AlertDialog.Builder(
					DiagramActivity.this).create();
			alertDialog.setMessage("Error: input file has been compromised.");
			alertDialog.show();
		} catch (IOException e) {
			AlertDialog alertDialog = new AlertDialog.Builder(
					DiagramActivity.this).create();
			alertDialog.setMessage("Error:" + e.getMessage());
			alertDialog.show();
			e.printStackTrace();
		}
		db = result.size() - 1;
		final LinearLayout layout = (LinearLayout) findViewById(R.id.grafikon);

		GraphViewData[] dataMotion = new GraphViewData[db];
		GraphViewData[] dataVel = new GraphViewData[db];
		GraphViewData[] dataAcc = new GraphViewData[db];

		int i = 0;
		ListIterator<double[]> it = result.listIterator();
		double[] temp = it.next();
		double maxX = temp[1];
		double maxY = temp[0];
		double maxT = 0;
		double maxV = 0;
		double maxA = 0;
		double[] res;
		for (; it.hasNext(); i++) {
			res = it.next();
			dataMotion[i] = new GraphViewData(res[1], res[2]);
			dataVel[i] = new GraphViewData(res[0], res[3]);
			dataAcc[i] = new GraphViewData(res[0], res[4]);
			if (res[0] > maxT)
				maxT = res[0];
			if (res[3] > maxV)
				maxV = res[3];
			if (res[4] > maxA)
				maxA = res[4];
		}
		GraphViewSeries DiagrMotion = new GraphViewSeries(dataMotion);
		final GraphView graphView = new LineGraphView(this, "Motion");
		graphView.addSeries(DiagrMotion);
		graphView.setViewPort(0, maxX);
		graphView.setScrollable(false);
		graphView.setScalable(false);
		graphView.setManualYAxisBounds(maxY, 0);

		GraphViewSeries DiagrVel = new GraphViewSeries(dataVel);

		final GraphView graphView2 = new LineGraphView(this, "Velocity");
		graphView2.addSeries(DiagrVel);
		graphView2.setViewPort(0, maxT);
		graphView2.setScrollable(false);
		graphView2.setScalable(false);
		graphView2.setManualYAxisBounds(maxV, 0);

		GraphViewSeries DiagrAcc = new GraphViewSeries(dataAcc);

		final GraphView graphView3 = new LineGraphView(this, "Acceleration");
		graphView3.addSeries(DiagrAcc);
		graphView3.setViewPort(0, maxT);
		graphView3.setScrollable(false);
		graphView3.setScalable(false);
		graphView3.setManualYAxisBounds(5, 0);

		Button motion = (Button) findViewById(R.id.motion);
		motion.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				layout.removeAllViews();
				layout.addView(graphView);
			}
		});

		Button acceleration = (Button) findViewById(R.id.acceleration);
		acceleration.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				layout.removeAllViews();
				layout.addView(graphView3);
			}
		});
		Button velocity = (Button) findViewById(R.id.velocity);
		velocity.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				layout.removeAllViews();
				layout.addView(graphView2);
			}
		});

	}
}